package com.github.caijh.mall.front.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.framework.wechat.model.WechatJsSdkConfig;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.front.response.LoginRespBody;
import com.github.caijh.mall.front.service.UserCenterService;
import com.github.caijh.mall.user.request.RegisterThirdUserRequest;
import com.github.caijh.mall.wechat.model.TemplateMessage;
import com.github.caijh.mall.wechat.model.WechatProgramMyTemp;
import com.github.caijh.mall.wechat.request.WechatProgramMyTempSearchRequest;
import com.github.caijh.mall.wechat.service.TemplateMessageService;
import com.github.caijh.mall.wechat.service.WeChatService;
import com.github.caijh.mall.wechat.service.WechatProgramMyTempService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController("WeChatFrontController")
@RequestMapping("api/front/wechat")
@Api(tags = "微信 -- 开放平台")
public class WeChatController extends BaseController {

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private UserCenterService userCenterService;

    @Autowired
    private TemplateMessageService templateMessageService;

    @Autowired
    private WechatProgramMyTempService wechatProgramMyTempService;

    @ApiOperation(value = "获取授权页面跳转地址")
    @GetMapping(value = "/authorize/get")
    public CommonResult<Object> get() {
        return CommonResult.success(weChatService.getAuthorizeUrl());
    }

    @ApiOperation(value = "微信登录公共号授权登录")
    @GetMapping(value = "/authorize/login")
    public CommonResult<LoginRespBody> login(@RequestParam(value = "spread_spid", defaultValue = "0", required = false) Integer spreadUid,
                                             @RequestParam(value = "code") String code) {
        return CommonResult.success(userCenterService.weChatAuthorizeLogin(code, spreadUid));
    }

    @ApiOperation(value = "微信登录小程序授权登录")
    @PostMapping(value = "/authorize/program/login")
    public CommonResult<LoginRespBody> minLogin(@RequestParam String code, @RequestBody @Validated RegisterThirdUserRequest request) {
        return CommonResult.success(userCenterService.weChatMiniLogin(code, request));
    }


    @ApiOperation(value = "获取微信公众号js配置")
    @GetMapping(value = "/config")
    @ApiImplicitParam(name = "url", value = "页面地址url", dataTypeClass = String.class)
    public CommonResult<WechatJsSdkConfig> configJs(@RequestParam(value = "url") String url) {
        return CommonResult.success(weChatService.getWechatJsSdkConfig(url));
    }

    @ApiOperation(value = "小程序获取授权logo")
    @GetMapping(value = "/getLogo")
    public CommonResult<JSONObject> getLogo() {
        return CommonResult.success(new JSONObject().fluentPut("logoUrl", userCenterService.getLogo()));
    }

    @ApiOperation(value = "查询微信模板信息")
    @GetMapping(value = "/info/{id}")
    public CommonResult<TemplateMessage> info(@PathVariable Integer id) {
        TemplateMessage templateMessage = templateMessageService.infoException(id);
        return CommonResult.success(templateMessage);
    }

    @ApiOperation(value = "订阅消息模板列表")
    @PostMapping(value = "/program/my/temp/list")
    public CommonResult<List<WechatProgramMyTemp>> programMyTempList(@RequestBody @Validated WechatProgramMyTempSearchRequest request, PageParams pageParams) {
        return CommonResult.success(wechatProgramMyTempService.getList(request, pageParams));
    }

}



